package engine.newUi
{
	import engine.data.ModelLocator;
	import flash.events.MouseEvent;
	import newx.com.control.window.NxWindowEvent;
	import engine.net.messages.CustomToClientFlag;
	import flash.display.MovieClip;
	import engine.net.event.CustomMsgEvent;
	import flash.display.DisplayObject;
	import newx.com.config.StaticIni;
	import flash.text.TextField;
	import engine.net.mgr.SystemSender;
	import engine.net.messages.CustomToServerFlag;
	import flash.display.SimpleButton;
	import engine.GameData;
	import engine.logic.walk.WalkLogic;
	import engine.display.newAttack.AttackLogic;

	public class WarfareLogic
	{
		private static var _self:WarfareLogic;
		private var _model:ModelLocator;
		private var _ui	:WarfareUI;
		private var _itemMC:MovieClip;
		
		private var _warfarVec:Vector.<WarfareList>;
		private var _isClick:Boolean;
		public static function  getInstance():WarfareLogic
		{
			if(_self ==null)
			{
				_self =  new WarfareLogic();
			}
			return _self;
		}
		public function WarfareLogic()
		{

		}
		public function set isClick(val:Boolean):void
		{
			_isClick = val;
		}
		public function get isClick():Boolean
		{
			return _isClick;
		}
		public function init():void
		{
			_warfarVec = new Vector.<WarfareList>();
			_model = ModelLocator.getInstance();	
			_ui	   = WarfareUI.getInstance();
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_ELITE_FB_REPORT_LIST),getList)
			initgroup();
		}
		private function initgroup():void
		{
			for(var i:int;i<_ui.viegroup.col*_ui.viegroup.row;i++)
			{
				_itemMC = _ui.viegroup.getItemByIndex(i);
				_itemMC["txt_0"].text ="";
				_itemMC["txt_1"].text ="查看";
				_itemMC["txt_2"].text ="";
				_itemMC["txt_3"].text ="";
				(_itemMC["txt_1"] as TextField).mouseEnabled =false
			}
		
				
		}
		public function BeforeShow():void
		{
			_ui.windowBG.titleIcon = "FactionActionUIPic";
			addEventListener();
			var fbname:String =StaticIni.getSwfLangStr(String( GameData.SceneConfig))
			 var showStr:String =StaticIni.getSwfLangStrVar("UIString_1000496",[fbname])
			var text:TextField = _ui.title.getChildByName("txt") as TextField;
			text.text =showStr
		}
		public function AfterHide():void
		{
			removeEventListener();
		}
		private function addEventListener():void
		{
			_ui.windowBG.addEventListener(NxWindowEvent.WINDOW_CLOSE,onClickClose);
			_ui.viegroup.addItemsEvent(MouseEvent.CLICK,onClickHandle);
			_ui.closebtn.addEventListener(MouseEvent.CLICK,onCloseHandle);
		}
		private function removeEventListener():void
		{
			_ui.windowBG.removeEventListener(NxWindowEvent.WINDOW_CLOSE,onClickClose);
			_ui.viegroup.removeItemsEvent(MouseEvent.CLICK,onClickHandle);
		}
		/**
		 *获取战报列表 
		 * @param e
		 * 
		 */		
		private function getList(e:CustomMsgEvent):void
		{
			_warfarVec = new Vector.<WarfareList>();
			var warfarArr:Array =[];
			warfarArr =e.pList;
			if(!_ui.created && warfarArr.length>0)
			{
				_ui.showOrHide();
			}

			for(var i:int=0;i<warfarArr.length;i+=4)
			{
				_warfarVec.push(new WarfareList(warfarArr[i],warfarArr[i+1],warfarArr[i+2],warfarArr[i+3]));
			}
			upGroup();
		}
		private function upGroup():void
		{
			for(var i:int =0;i<_ui.viegroup.col*_ui.viegroup.row;i++)
			{
				var itemMc:MovieClip =_ui.viegroup.getItemByIndex(i);
				if(i>=_warfarVec.length)
				{
					itemMc.visible=false;
				}
				else
				{
					itemMc.visible=true;
					itemMc["txt_0"] .text =_warfarVec[i].name;
					itemMc["txt_3"] .text=_warfarVec[i].lv;
					itemMc["txt_2"] .text =StaticIni.getSwfLangStr("Profession_"+_warfarVec[i].prof);
					itemMc["data"] =_warfarVec[i].key;
				}
				
			}
		}
		private function onCloseHandle(e:MouseEvent):void
		{
			_ui.hide();
		}
		private function onClickClose(e:NxWindowEvent):void
		{
			_ui.hide();
		}
		public function  set enable(val:Boolean):void
		{
			_ui.viegroup.mouseChildren=val;
		}
		private function onClickHandle(e:MouseEvent):void
		{
			var mc:MovieClip = e.currentTarget as MovieClip
			var btn:SimpleButton =e.target as SimpleButton;
			if(mc && mc["data"] && btn && !AttackLogic.getInstance().fighting )
			{
				GameData.Role.killMove();
				isClick=true;
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_REQUEST_ELITE_FB_DBFIGHT_REPORT,int(mc["data"]))
			}
				
		}
	}
}
class WarfareList
{
	public var name:String;
	public var lv:int;
	public var prof:int;  //职业
	public var key:int;  //战报的congfid
	public function  WarfareList(name:String,lv:int,prof:int,key:int):void
	{
		this.name= name;
		this.lv =lv;
		this.prof =prof;
		this.key =key;
	}
}